﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace Woodbase.CodeBase.WVR.Objects
{
    public class Users : List<User>
    {
        public Users() { }
        public Users(List<User> user) : base(user) { }
    }


    [XmlType("User")]
    [Serializable()]
    public class User : ISerializable
    {
        [XmlAttribute("givenname")]
        public string GivenName { get; set; }
        [XmlAttribute("lastname")]
        public string LastName { get; set; }
        [XmlAttribute("active")]
        public bool Active { get; set; }
        [XmlAttribute("blacklisted")]
        public bool Blacklisted { get; set; }
        [XmlAttribute("city")]
        public string City { get; set; }
        [XmlAttribute("phone")]
        public string Phone { get; set; }
        [XmlAttribute("street")]
        public string Street { get; set; }
        [XmlAttribute("number")]
        public string Number { get; set; }
        [XmlAttribute("user-id")]
        public string UserId { get; set; }
        [XmlAttribute("zip-code")]
        public string ZipCode { get; set; }
        [XmlElement("movies")]
        public List<Movie> Movies { get; set; }

        //Serialization function.
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            //You can use any custom name for your name-value pair. But make sure you
            // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
            // then you should read the same with "EmployeeId"
            info.AddValue("GivenName", GivenName);
            info.AddValue("LastName", LastName);
        }

        [XmlNamespaceDeclarations()]
        public XmlSerializerNamespaces xmlsn
        {
            get
            {
                XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
                xsn.Add("wvru", "http://ns.woodbase.dk/User.xsd");
                return xsn;
            }
            set
            {
                //Just provide an empty setter. 
            }
        }


        /// <summary>
        /// TODO: Fix movies rented!!!
        /// </summary>
        /// <returns></returns>
        public XElement ToXml()
        {
            XNamespace ns = XNamespace.Get("http://ns.woodbase.dk/User.xsd");
            XElement x = new XElement(ns+"user", 
                                new XAttribute("givenname", GivenName), 
                                new XAttribute("lastname", LastName), 
                                new XAttribute("active", Active), 
                                new XAttribute("blacklisted", Blacklisted),
                                new XAttribute("city", City),
                                new XAttribute("zipcode", ZipCode),
                                new XAttribute("phone", Phone),
                                new XAttribute("street", Street),
                                new XAttribute("streetnumber", Number),
                                new XAttribute("userid", UserId)
                                );
            return x;
        }
    }
}